package it.storm.solution;

/**
 * 419. 甲板上的战舰
 * https://leetcode-cn.com/problems/battleships-in-a-board/
 */
public class Solutions_419 {
    public static void main(String[] args) {
        char[][] board = {{'X', '.', '.', 'X'},
                        {'.', '.', '.', 'X'},
                        {'.', '.', '.', 'X'}};  // output: 1

        int result = countBattleships(board);
        System.out.println(result);
    }

    public static int countBattleships(char[][] board) {
        // 题目说明了：每个战舰之间至少会有一个空位将它们分开。战舰只能水平或者垂直放置
        int ans = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == 'X') {
                    if ((i == 0 || board[i - 1][j] != 'X') && (j == 0 || board[i][j - 1] != 'X')) {
                        // 左边及上边是空位，说明该战舰是整个战舰的左上角部分
                        ans ++;
                    }
                }
            }
        }
        return ans;
    }
}
